
var express = require('express');
var router = express.Router();
var models = require('../models');
var uuid = require("uuid");
var logger = require("../middleware/logger");
var needauth = require("../middleware/needauth");
var discuss = require('../services/discuss');

router.post('/create', function (req, res) {
    var _obj = {
        discussid:  uuid.v1().replace(/-/g,""),
        listid: req.pack.req.data.listid || null,
        noteid: req.pack.req.data.noteid || null,
        userid: req.pack.req.data.userid || null,
        visitor: req.pack.req.data.visitor || null,
        type: req.pack.req.data.type || null,
        likes: req.pack.req.data.likes || 0,
        lose: req.pack.req.data.lose || null,
        lose_content: req.pack.req.data.lose_content || null,
        sort: req.pack.req.data.sort || null,
        content: req.pack.req.data.content || null,
        ext: req.pack.req.data.ext || null,
    };
    discuss.create(_obj)
        .then(function () {
            req.pack.setMsg({type: 1, data: '创建成功'});
            res.send(req.pack);
        })
        .catch(function (err) {
            req.pack.setStatus(-1);
            req.pack.setMsg({type: 2, data: err.message});
            res.send(req.pack);
        });
});

router.post('/delete', function (req, res) {
        discuss.delete(req.pack.req.data.discussid)
            .then(function () {

                req.pack.setMsg({type: 1, data: '删除成功'});
                res.send(req.pack);

            })
            .catch(function (err) {
                req.pack.setStatus(-1);
                req.pack.setMsg({type: 2, data: err.message});
                res.send(req.pack);
            });
});

router.post('/update', function (req, res) {
    var _obj = {
        discussid: req.pack.req.data.discussid || null,
        listid: req.pack.req.data.listid || null,
        noteid: req.pack.req.data.noteid || null,
        userid: req.pack.req.data.userid || null,
        visitor: req.pack.req.data.visitor || null,
        type: req.pack.req.data.type || null,
        sort: req.pack.req.data.sort || null,
        ext: req.pack.req.data.ext || null,
    };
    discuss.update(_obj)
        .then(function () {
            req.pack.setMsg({type: 1, data: '更新成功'});
            res.send(req.pack);
        })
        .catch(function (err) {
            req.pack.setStatus(-1);
            req.pack.setMsg({type: 2, data: err.message});
            res.send(req.pack);
        });
});


router.post('/search', function (req, res) {
    var key = req.pack.req.data.key;
    var value = '%' + req.pack.req.data.value + '%';
    var st = req.pack.req.data.startDatetime || null;
    var et = req.pack.req.data.endDatetime || null;

    var obj = {};
    var obj2 = {};
    obj2['$like'] = value;
    obj[key] = obj2;

    if (st && et) {
        obj2['$between'] = [st, et];
        obj['createdAt'] = obj2;
    }
    var objA={},objC={},objs={};
    var k=[{
        model: models.user,
        attributes: ['title']
    }];

    if(key== 'abstract'){
        objA=obj;
        k.push({
            model: models.notes,
            attributes: ['title', 'picture', 'abstract'],
            where: objA
        })
    }else if(key == 'content'){
        objC=obj
        k.push({
            model: models.noted,
            attributes: ['nickname', 'createdAt','content'],
            where: objC
        })
    }else{
        objs=obj;
    }

    models.discuss.findAll({
            include: k,
            where:objs
        }
    ).then(function (data) {
        if (data) {
            req.pack.setResult(data);
            res.send(req.pack);
        } else {
            req.pack.setMsg({type: 4, data: '找不到相应信息'});
        }
    }).catch(function (err) {
        req.pack.setStatus(-1);
        req.pack.setMsg({type: 2, data: err.message});
        res.send(req.pack);
    });
});

router.post('/listPage', function (req, res) {
    var offset = parseInt(req.pack.req.data.offset);
    var limit = parseInt(req.pack.req.data.limit) || 20;
    var st = req.pack.req.data.startDatetime || null;
    var et = req.pack.req.data.endDatetime || null;
    // var order = req.pack.req.data.order ||null;
    discuss.listPageList(offset, limit, st, et,{
        include: [{
            model: models.user,
            attributes: ['title','picture']
        }, {
            model: models.notes,
            attributes: ['title', 'picture', 'abstract']
        }]
    })
        .then(function (data) {
            req.pack.setResult(data)
            res.send(req.pack);
        })
        .catch(function (err) {

            req.pack.setStatus(-1);
            req.pack.setMsg({type: 2, data: err.message});
            res.send(req.pack);
        });
});

router.post('/detail', function (req, res) {

    discuss.findOne(req.pack.req.data.discussid)
        .then(function (result) {
            req.pack.setResult(result);
            res.send(req.pack);
        })
        .catch(function (err) {
            req.pack.setStatus(-1);
            req.pack.setMsg({type: 2, data: err.message});
            res.send(req.pack);
        });
});

router.post('/join', function (req, res) {
    models.discuss.findAll({
        include: [{
            model: models.user,
            attributes: ['title']
        }, {
            model: models.notes,
            attributes: ['title', 'picture', 'abstract']
        },{
            model: models.noted,
            attributes: ['nickname', 'createdAt']
        }],
        where: {
            userid:req.pack.req.data.userid
        }
    }).then(function (data) {
        if (data) {
            req.pack.setResult(data);
            res.send(req.pack);
        } else {
            req.pack.setMsg({type: 4, data: '找不到相应信息'});
        }
    }).catch(function (err) {
        req.pack.setStatus(-1);
        req.pack.setMsg({type: 2, data: err.message});
        res.send(req.pack);
    });
});

router.post('/count', function (req, res) {
    var where = {userid:req.pack.req.data.userid};
    models.discuss.count({where: where})
        .then(function (result) {
            req.pack.setResult(result);
            res.send(req.pack);
        })
        .catch(function (err) {
            req.pack.setStatus(-1);
            req.pack.setMsg({type: 2, data: err.message});
            res.send(req.pack);
        });
});
//修改通知
router.post('/countLike', function (req, res) {
    var whereTo={}
    if(req.pack.req.data.userid)
    {
        whereTo={userid:req.pack.req.data.userid}
    }else if(req.pack.req.data.listid){
        whereTo={listid:req.pack.req.data.listid}
    }else if(req.pack.req.data.noteid){
        whereTo={noteid:req.pack.req.data.noteid}
    }
    models.discuss.sum("likes",{
        where:whereTo
        }
        ).then(function (result) {
            req.pack.setResult(result);
            res.send(req.pack);
        })
        .catch(function (err) {
            req.pack.setStatus(-1);
            req.pack.setMsg({type: 2, data: err.message});
            res.send(req.pack);
        });
});
router.post('/findOne', function (req, res) {
    models.discuss.findAll({where:{listid:req.pack.req.data.listid}})
        .then(function (result) {
            req.pack.setResult(result);
            res.send(req.pack);
        })
        .catch(function (err) {
            req.pack.setStatus(-1);
            req.pack.setMsg({type: 2, data: err.message});
            res.send(req.pack);
        });
});
router.post('/updateZan', function (req, res) {
    var _obj = {
        discussid:req.pack.req.data.discussid,
        listid: req.pack.req.data.listid || null,
        likes: req.pack.req.data.likes || 0
    };
    discuss.update(_obj)
        .then(function () {
            req.pack.setMsg({type: 1, data: '更新成功'});
            res.send(req.pack);
        })
        .catch(function (err) {
            req.pack.setStatus(-1);
            req.pack.setMsg({type: 2, data: err.message});
            res.send(req.pack);
        });
});
module.exports = router;